<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/6/6
 * Time: 15:51
 */

namespace App\Rep;


use App\Models\Appendance;
use App\Models\AppendanceLog;
use App\Models\Employee;

class AppendanceLogRep
{

    /**
     * 获取考勤列表
     * @param $params
     * 1:正常2：未打卡3：迟到4：休息5：请假6:早退
     */
    public function getAppendanceLogListRep($params){
        //员工列表
        $employee_user_list = Employee::Search($params)->select('id','user_id','employee_name')->paginate($params['per_page']??100);
        $start_time = $params['year_month']??date("Y-m")."-01";
        $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day"));
        $period = \Carbon\CarbonPeriod::create($start_time, $end_time);
        $date_list = [];
        foreach ($period as $key => $date) {
            $date_list[] = $date->format('Y-m-d');
        }
        foreach($employee_user_list as $k=>$v){
            foreach($date_list as $kk=>$vv){
                $log[$kk]['date'] = date("d",strtotime($vv));
                $first = $this->getUserAppendanceLogInfo($v->user_id,$vv,1,'asc');
                if($first){
                    $log[$kk]['first'] = $first->status;
                    $log[$kk]['first_time'] = $first->appendance_time??'';
                }else{
                    $log[$kk]['first'] = 2;
                    $log[$kk]['first_time'] = '';
                }

                $two = $this->getUserAppendanceLogInfo($v->user_id,$vv,2,'desc');
                if($two){
                    $log[$kk]['two'] = $two->status;
                    $log[$kk]['two_time'] = $two->appendance_time??'';
                }else{
                    $log[$kk]['two'] = 2;
                    $log[$kk]['two_time'] = '';
                }

                $three = $this->getUserAppendanceLogInfo($v->user_id,$vv,3,'asc');
                if($three){
                    $log[$kk]['three'] = $three->status;
                    $log[$kk]['three_time'] = $three->appendance_time??'';
                }else{
                    $log[$kk]['three'] = 2;
                    $log[$kk]['three_time'] = '';
                }

                $fore = $this->getUserAppendanceLogInfo($v->user_id,$vv,4,'desc');
                if($fore){
                    $log[$kk]['fore'] = $fore->status;
                    $log[$kk]['fore_time'] = $fore->appendance_time??'';
                }else{
                    $log[$kk]['fore'] = 2;
                    $log[$kk]['fore_time'] = '';
                }

            }
            $employee_user_list[$k]['appendance_log'] = $log;
        }
        return $employee_user_list;
    }


    public function getUserAppendanceLogInfo($user_id,$date,$type,$order_by){
        return AppendanceLog::where('user_id',$user_id)
            ->where('date',$date)
            ->where('type',$type)
            ->orderBy('appendance_time',$order_by)
            ->first();
    }

    /**
     * 考勤打卡
     * @param $params
     */
    public function addAppendanceLogRep($params){
        $appendance = Appendance::find(1)->toArray();
        $type = $params['type'];
        $field = $this->getAppendanceField($type);
        $time_field = $appendance[$field];
        $date = date('H:i:s');
        $status = $this->getAppendanceStatus($type,$time_field,$date);

        $model = new AppendanceLog();
        $model->type = $params['type'];
        $model->user_id = $params['user_id'];
        $model->date = date('Y-m-d');
        $model->appendance_time = date('Y-m-d H:i:s');
//        if($type == 6){
//            $model->overtime = strtotime($time_field)-time();
//        }
        $model->status = $status;
        $model->save();
        return $model;
    }

    public function getAppendanceField($type){
        switch ($type)
        {
            case 1:
                return 'am_office_hours';
                break;
            case 2:
                return "am_closing_time";//case2
                break;
            case 3:
                return "pm_office_hours";//case3
                break;
            case 4:
                return "pm_closing_time";//case3
                break;
            case 5:
                return "ot_office_hours";//case3
                break;
            case 6:
                return "ot_closing_time";//case3
                break;
            default:
                return "am_office_hours";
        }
    }


    //1:正常2：未打卡3：迟到4：休息5：请假6:早退7:加班
    public function getAppendanceStatus($type,$time_field,$date){
        $status = 1;
        if(in_array($type,[1,3])){
            if($time_field<$date){
                $status = 3;
            }
        }elseif (in_array($type,[2,4])){
            if($time_field>$date){
                $status = 6;
            }
        }elseif (in_array($type,[5,6])){
            $status = 7;
        }
        return $status;
    }

    /**
     * 补考勤
     * @param $params
     */
    public function supplementAppendanceLogRep($params){
        $model = new AppendanceLog();
        $appendance = Appendance::find(1)->toArray();
        $date = date("H:i:s",strtotime($params['appendance_time']));
        $type = $params['type'];
        $field = $this->getAppendanceField($type);
        $time_field = $appendance[$field];
        $status = $this->getAppendanceStatus($type,$time_field,$date);
        $model->user_id = $params['users_id'];
        $model->type = $type;
        $model->date = explode('',$params['appendance_time'])[0];
        $model->appendance_time = $params['appendance_time'];
        $model->status = $status;
        $model->save();
        return $model;
    }

    /**
     * 获取加班列表
     * @param $params
     */
    public function getWorkOvertimeListRep($params){
        //员工列表
        $employee_user_list = Employee::Search($params)->select('id','user_id','employee_name')->paginate($params['per_page']??100);
        $start_time = $params['year_month']??date("Y-m")."-01";
        $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day"));
        $period = \Carbon\CarbonPeriod::create($start_time, $end_time);
        $date_list = [];
        foreach ($period as $key => $date) {
            $date_list[] = $date->format('Y-m-d');
        }
        foreach($employee_user_list as $k=>$v){
            foreach($date_list as $kk=>$vv){
                $log[$kk]['date'] = date("d",strtotime($vv));
                $over = $this->getUserAppendanceLogInfo($v->user_id,$vv,6,'asc');
                if($over){
                    $log[$kk]['over'] = $over->status;
                    $log[$kk]['appendance_time'] = $over->appendance_time??'';
                    $log[$kk]['overtime'] = number_format($over->overtime/60, 2);
                }
            }
            $employee_user_list[$k]['appendance_log'] = $log;
        }
        return $employee_user_list;
    }
}
